<?php

use yii\db\Migration;
use yii\db\Schema;


class m180319_064253_CREATE_DB extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {

        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';

        // 游轮基础信息
        $this->createTable('cruise', [
            "id" =>         Schema::TYPE_PK,
            "name" =>       Schema::TYPE_STRING.    "(128) NULL COMMENT '名称'",
            "en_name" =>    Schema::TYPE_STRING.    "(255) NULL COMMENT '英文名称'",
            "weight" =>     Schema::TYPE_DECIMAL.   "(10,2) DEFAULT NULL COMMENT '重量  吨'",
            "num" =>        Schema::TYPE_INTEGER.   "(9) DEFAULT  '0' COMMENT '载客人数 人'",
            "length" =>     Schema::TYPE_INTEGER.   "(9) DEFAULT  '0' COMMENT '长度 米'",
            "width" =>      Schema::TYPE_INTEGER.   "(9) DEFAULT  '0' COMMENT '宽度 米'",
            "room" =>       Schema::TYPE_INTEGER.   "(9) DEFAULT  '0' COMMENT '客房数 间'",
            "ceil" =>       Schema::TYPE_INTEGER.   "(9) DEFAULT  '0' COMMENT '楼层数 层'",
            "speed" =>      Schema::TYPE_INTEGER.   "(6) DEFAULT  '0' COMMENT '船速 节'",
            "first" =>      Schema::TYPE_STRING.    "(32) NULL COMMENT '首航时间 2014 年'",
            "code" =>       Schema::TYPE_STRING.    "(8) null COMMENT '游轮简码'",
            "desc" =>       Schema::TYPE_STRING.    "(1024) NULL COMMENT '描述'",
            "status" =>     Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2禁用'",
            "user_id" =>    Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" => Schema::TYPE_DATETIME. " NULL",
            "updated_at" => Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);


        // 舱房信息
        $this->createTable('room', [
            "id" =>         Schema::TYPE_PK,
            "cruise_id" =>  Schema::TYPE_STRING.    "(11) NOT NULL COMMENT '游轮编号'",
            "name" =>       Schema::TYPE_STRING.    "(128) NULL COMMENT '名称'",
            "code" =>       Schema::TYPE_STRING.    "(16) NULL COMMENT '简码'",
            "windows" =>    Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1 无窗 2 有窗'",
            "area" =>       Schema::TYPE_INTEGER.   "(8) DEFAULT  '0' COMMENT '面积  平米'",
            "num" =>        Schema::TYPE_STRING.    "(16) NULL  COMMENT '入住人数 '",
            "ceil" =>       Schema::TYPE_STRING.    "(64) NULL  COMMENT '所在楼层 JSON [1,2,4,5]'",
            "desc" =>       Schema::TYPE_STRING.    "(512) NULL COMMENT '描述'",
            "status" =>     Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2禁用'",
            "user_id" =>    Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" => Schema::TYPE_DATETIME. " NULL",
            "updated_at" => Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);
        $this->createIndex("room_cruise", "room", "cruise_id");


        // 使用舱房信息
        $this->createTable('room_use', [
            "id" =>         Schema::TYPE_PK,
            "room_id" =>    Schema::TYPE_STRING.    "(11) NOT NULL COMMENT '舱房编号'",
            "num" =>       Schema::TYPE_STRING.    "(128) NULL COMMENT '舱房入住人数'",
            "code" =>       Schema::TYPE_STRING.    "(16) NULL COMMENT '房间号'",
            "download" =>    Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '是否下载 0 修改  1 已下载'",
            "status" =>     Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2禁用'",
            "user_id" =>    Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" => Schema::TYPE_DATETIME. " NULL",
            "updated_at" => Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);

        //客人信息
        $this->createTable('customer', [
            "id" =>           Schema::TYPE_PK,
            "room_use_id"=>   Schema::TYPE_INTEGER.   "(9) NOT NULL COMMENT '所属房间'",
            "first_name" =>   Schema::TYPE_STRING.    "(32) NULL COMMENT '姓'",
            "last_name" =>    Schema::TYPE_STRING.    "(32) NULL COMMENT '名'",
            "en_first_name"=> Schema::TYPE_STRING.    "(32) NULL COMMENT '英文姓'",
            "en_last_name"=>  Schema::TYPE_STRING.    "(32) NULL COMMENT '英文名'",
            "pinyin"=>        Schema::TYPE_STRING.    "(255) NULL COMMENT '名字拼音'",
            "gender" =>       Schema::TYPE_INTEGER.   "(1) DEFAULT  '0' COMMENT '1 男 2 女'",
            "birthday" =>     Schema::TYPE_DATE.      " NULL COMMENT '出生日期'",
            "country"=>       Schema::TYPE_STRING.    "(64) NULL COMMENT '国籍'",
            "passport"=>      Schema::TYPE_STRING.    "(255) NULL COMMENT '护照号码'",
            "passport_add"=>  Schema::TYPE_STRING.    "(255) NULL COMMENT '护照签发地'",
            "passport_date"=> Schema::TYPE_DATE.      " NULL COMMENT '护照有效期'",
            "passport_type"=> Schema::TYPE_INTEGER.   "(1) DEFAULT  '0' COMMENT '护照类型 1 原件 2 复印件'",
            "phone"=>         Schema::TYPE_STRING.    "(64) NULL COMMENT '手机号'",
            "remark"=>        Schema::TYPE_STRING.    "(255) NULL COMMENT '备注说明'",
            "status" =>       Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2禁用'",
            "user_id" =>      Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" =>   Schema::TYPE_DATETIME.  " NULL",
            "updated_at" =>   Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);

        // 邮轮房间编号
        $this->createTable('room_code', [
            "id" =>         Schema::TYPE_PK,
            "cruise_id" => Schema::TYPE_INTEGER. "(11) NULL COMMENT '邮轮ID' ",
            "deck" => Schema::TYPE_INTEGER. "(11) NULL COMMENT '甲板层数' ",
            "code" =>    Schema::TYPE_STRING.    "(20) NULL COMMENT '房间号'",
            "type" => Schema::TYPE_INTEGER. "(11) DEFAULT  '1' COMMENT '房型' ",
            "type_code" =>    Schema::TYPE_STRING.    "(20) NULL COMMENT '房型代码'",
            "num" =>  Schema::TYPE_INTEGER. "(4) NULL COMMENT '房间入住最大人数'",
            "link_room" =>    Schema::TYPE_STRING.    "(20) NULL COMMENT '连通房'",

            "location" =>  Schema::TYPE_STRING.  "(522) NULL COMMENT 'svg坐标'",

            "remark" => Schema::TYPE_STRING. "(256) NULL COMMENT '备注'",
            "status" =>       Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常 2禁止'",
            "user_id" =>      Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" =>   Schema::TYPE_DATETIME.  " NULL",
            "updated_at" =>   Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);
        
        $this->createIndex('room_code','room_code',['cruise_id','code'],true);

        //使用的邮轮房间编号
        $this->createTable('room_code_use', [
            "id" =>         Schema::TYPE_PK,
            "cruise_id" => Schema::TYPE_INTEGER. "(11) NULL COMMENT '邮轮ID' ",
            "room_code_id" => Schema::TYPE_INTEGER. "(11) NULL COMMENT 'room Code ID' ",
            "code" =>    Schema::TYPE_STRING.    "(20) NULL COMMENT '房间号'",
            "is_use" => Schema::TYPE_INTEGER. "(1) DEFAULT '1' COMMENT '是否使用 1 未使用 2使用' ",
            "user_id" =>      Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '管理员编号'",
            "created_at" =>   Schema::TYPE_DATETIME.  " NULL",
            "updated_at" =>   Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m180319_064253_CREATE_DB cannot be reverted.\n";
        $this->dropTable('cruise');
        $this->dropTable('room');
        $this->dropTable('room_use');
        $this->dropTable('customer');
        $this->dropTable('room_code');
        $this->dropTable('room_code_use');
        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m180319_064253_CREATE_DB cannot be reverted.\n";

        return false;
    }
    */
}
